import * as Cesium from 'cesium'
import gsap from "gsap";
import * as dat from 'dat.gui'
export async function modifyBuild(viewer) {
  let cusshader = new Cesium.CustomShader({
    lightingModel: Cesium.LightingModel.UNLIT,
    uniforms: {
      u_time: {
        value: 0, // initial value
        type: Cesium.UniformType.FLOAT
      },
    },
    fragmentShaderText: `
          void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material)
          {
            vec3 positionEC = fsInput.attributes.positionEC;
            vec4 postion = czm_inverseModelView * vec4(positionEC, 1.0);
            float strleng = postion.z/200.0;
            material.diffuse = vec3(strleng, 0.3 * strleng, strleng);
            
            // fract(x)返回x的小数部分
            float time = fract(czm_frameNumber/(60.0 * 7.0)); // 0-1中间数 柱子移动速度
            time = abs(time - 0.5) * 2.0; // -1 到 1 往返 
            // 将x值钳于minVal和maxVal之间,意思就是当x<minVal时返回minVal,当x>maxVal时返回maxVal,当x在minVal和maxVal之间时,返回x
            float diff = abs(clamp(postion.z/500.0,0.0,1.0) - time); // 接近为0或者1
            // 该step(edge,x)函数中,如果x < edge,返回0.0,否则返回1.0。
            diff = step(0.01, diff); // 柱子大小 0或者1

            material.diffuse += vec3(0.5) * (1.0-diff);
            
          }
          `,
  })
  let tiles3d = await Cesium.Cesium3DTileset.fromIonAssetId(96188, {
    customShader: cusshader
  });
  viewer.scene.primitives.add(tiles3d)
  tiles3d.outlineColor = new Cesium.Color(0, 0.5, 1.0, 0)

  const gui = new dat.GUI()
  gui.add(cusshader.uniforms.u_time, 'value', 0, 1)
  // cusshader.setUniform("u_time", 1)
    // gsap.to(cusshader.uniforms.u_time, {
    //   value: 1,
    //   duration: 1,
    //   repeat: -1,
    //   ease: "linear",
    // });
  // tiles3d.style = new Cesium.Cesium3DTileStyle({
  //   show: "${feature['name']} !== '广州塔'",
  // });

}